package com.campus.merchant.controller;

import cn.hutool.core.util.StrUtil;
import com.campus.common.bean.common.Resp;
import com.campus.merchant.service.WechatAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/wechat")
public class WechatAuthController {
    @Autowired
    private WechatAuthService wechatAuthService;

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    /**
     * 微信小程序静默登录
     * @param params 包含微信登录code
     * @return token和用户信息
     */
    @PostMapping("/wxLogin")
    public Resp wxLogin(@RequestBody Map<String, String> params) {
        String code = params.get("code");
        if (StrUtil.isBlank(code)) {
            return Resp.error("code不能为空");
        }
        return wechatAuthService.wxLogin(code);
    }

    /**
     * 刷新token
     * @param params 包含refreshToken
     * @return 新accessToken和refreshToken
     */
    @PostMapping("/refreshToken")
    public Resp refreshToken(@RequestBody Map<String, String> params) {
        String refreshToken = params.get("refreshToken");
        if (StrUtil.isBlank(refreshToken)) {
            return Resp.error("refreshToken不能为空");
        }
        return wechatAuthService.refreshToken(refreshToken);
    }
}